package com.dabai.iot.core.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dabai.common.core.domain.R;
import com.dabai.common.mybatis.core.page.PageQuery;
import com.dabai.common.mybatis.core.page.TableDataInfo;
import com.dabai.common.web.core.BaseController;
import com.dabai.iot.common.enums.FunctionType;
import com.dabai.iot.core.dto.ThingModelItemInput;
import com.dabai.iot.core.dto.ThingModelItemQuery;
import com.dabai.iot.core.dto.ThingModelItemUpdate;
import com.dabai.iot.core.entity.ThingModelItemEntity;
import com.dabai.iot.core.service.IThingModelItemService;
import com.dabai.iot.core.vo.ThingModelItemVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 物模型属性控制器
 *
 * @author kev1n
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/thingModel/item")
public class ThingModelItemController extends BaseController {

    private final IThingModelItemService thingModelItemService;

    /**
     * 新增物模型属性
     */
    @PostMapping("/add")
    @Operation(summary = "新增物模型属性")
    public R<Void> add(@Valid @RequestBody ThingModelItemInput thingModelItemInput) {
        return toAjax(thingModelItemService.addThingModelItem(thingModelItemInput));
    }

    /**
     * 修改物模型属性
     */
    @PostMapping("/update")
    @Operation(summary = "修改物模型属性")
    public R<Void> update(@Valid @RequestBody ThingModelItemUpdate thingModelItemUpdate) {
        return toAjax(thingModelItemService.updateThingModelItem(thingModelItemUpdate));
    }

    /**
     * 删除物模型属性
     */
    @PostMapping("/remove")
    @Operation(summary = "删除物模型属性", description = "传入主键id")
    public R<Void> remove(@Parameter(description = "主键id", required = true) @RequestParam Long id) {
        return toAjax(thingModelItemService.removeThingModelItem(id));
    }

    /**
     * 物模型属性详情
     */
    @GetMapping("/detail")
    @Operation(summary = "物模型属性详情", description = "传入主键id")
    public R<ThingModelItemEntity> detail(@RequestParam Long id) {
        ThingModelItemEntity detail = thingModelItemService.getById(id);
        return R.ok(detail);
    }

    /**
     * 物模型属性下拉数据源
     */
    @GetMapping("/list")
    @Operation(summary = "物模型属性下拉数据源", description = "用于物模型属性下拉选择，传入物模型功能模块主键：blockId")
    public R<List<ThingModelItemEntity>> select(@RequestParam Long blockId) {
        return R.ok(thingModelItemService.lambdaQuery()
            .eq(ThingModelItemEntity::getBlockId, blockId)
            .eq(ThingModelItemEntity::getFunctionType, FunctionType.property.getValue())
            .list()
        );
    }

    /**
     * 物模型属性分页
     */
    @GetMapping("/page")
    @Operation(summary = "物模型属性分页", description = "传入对应查询参数")
    public TableDataInfo<ThingModelItemVO> page(PageQuery pageQuery, ThingModelItemQuery modelItemQuery) {
        IPage<ThingModelItemVO> pages = thingModelItemService.getThingModelItemPage(pageQuery.build(), modelItemQuery);
        return TableDataInfo.build(pages);
    }

    /**
     * 根据 blockId 获取列表
     */
    @GetMapping("/queryList")
    @Operation(summary = "根据 blockId 获取列表", description = "传入blockId")
    public R<List<ThingModelItemEntity>> selectByBlockId(@RequestParam Long blockId) {
        List<ThingModelItemEntity> list = thingModelItemService.getItemListByBlockId(blockId);
        return R.ok(list);
    }

    /**
     * 分页1
     */
//    @GetMapping("/page1")
//    @Operation(summary = "分页1", description = "传入对应查询参数")
//    public R<IPage<ThingModelItemEntity>> page1(PageQuery pageQuery, ThingModelItemQuery modelItemQuery) {
//        IPage<ThingModelItemEntity> pages = thingModelItemService.getThingModelItemPage1(pageQuery.build(), modelItemQuery);
//        return R.ok(pages);
//    }


//    @GetMapping("/checkPropertyExist")
//    @Operation(summary = "查看 默认物模型属性是否存在")
//    public RP checkPropertyExist(@RequestParam Long productId) {
//        return RP.data(thingModelItemService.checkDefaultModelPropertyExist(productId));
//    }

}
